package cn.myfirefly.moming.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.DepartmentDao;
import cn.myfirefly.moming.common.dao.DepartmentStaffDao;
import cn.myfirefly.moming.common.dao.PositionDao;
import cn.myfirefly.moming.common.dao.StaffDao;
import cn.myfirefly.moming.common.dao.UserDao;
import cn.myfirefly.moming.common.entity.MMDepartment;
import cn.myfirefly.moming.common.entity.MMDepartmentStaff;
import cn.myfirefly.moming.common.entity.MMPosition;
import cn.myfirefly.moming.common.entity.MMStaff;
import cn.myfirefly.moming.common.entity.MMUser;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.model.StaffFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class StaffService {
//	private Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private StaffDao staffDao;
	
	@Autowired
	private DepartmentStaffDao departmentStaffDao;
	
	@Autowired
	private DepartmentDao departmentDao;
	
	@Autowired
	private PositionDao positionDao;
	
	@Autowired
	private UserDao userDao;
	
	public PageResultDto<StaffDto> getList(StaffFilterDto dto) {
		
		return staffDao.getList(dto);
	}
	
	public PageResultDto<StaffDto> getList(StaffDto dto) {
		
		return staffDao.getList(dto);
	}
	
	public String save(StaffDto dto) {
		MMStaff staff = staffDao.getStaffByNo(dto.getStaffNo());
		// 更新
		if (StrUtil.notBlank(dto.getStaffSn())) {
			if (staff == null) {
				return "员工信息不存在，请刷新页面后重试。";
			}
			MMStaff tempStaff = staffDao.get(dto.getStaffSn());
			if (staff != null && tempStaff != null && !staff.getStaffSn().equals(tempStaff.getStaffSn())) {
				return "员工编号已存在。";
			}
		} else {
		// 新增
			if (staff != null) {
				return "用户编号已存在。";
			} 
			staff = new MMStaff();
			staff.setCreateBy(dto.getModifyBy());
			staff.setCreateDate(dto.getModifyDate());
		}
		BeanUtil.copyBeans(dto, staff);
		if (dto.getPositionSn() != null) {
			staff.setPosition(dto.getPositionSn().toString());
		} else {
			staff.setPosition(null);
		}
		staff.setUserSn(dto.getUserSn());
		Integer staffSn = staffDao.save(staff);
		
		// 删除部门和员工关联数据
		departmentStaffDao.delByStaffSn(staffSn);
		// 新增部门和员工关联数据
		if (StrUtil.notBlank(dto.getDepartmentSn())) {
			MMDepartmentStaff deptStaff = new MMDepartmentStaff();
			deptStaff.setDepartmentSn(Integer.valueOf(dto.getDepartmentSn()));
			deptStaff.setStaffSn(staffSn);
			departmentStaffDao.save(deptStaff);
		}
		
		return Const.ResultCode.SUCCESS;
	}
	
	public StaffDto getStaff(Integer staffSn) {
		MMStaff staff = staffDao.get(staffSn);
		StaffDto staffDto = BeanUtil.copyBeans(StaffDto.class, staff);
		// 取得员工部门数据
		MMDepartment dept = departmentStaffDao.getDepartment(staffSn);
		if (dept != null) {
			staffDto.setDepartmentSn(dept.getDepartmentSn());
			staffDto.setDepartmentName(getDepartmentNamePath("", dept));
		} else {
			staffDto.setDepartmentSn(null);
			staffDto.setDepartmentName("");
		}
		// 取得员工职位信息
		if (staff.getPosition() != null) {
			MMPosition position = positionDao.get(Integer.valueOf(staff.getPosition()));
			if (position != null) {
				staffDto.setPositionSn(position.getPositionSn());
				staffDto.setPositionName(position.getPositionName());
			}
		}
		// 取得员工登录账号
		if (staff.getUserSn() != null) {
			MMUser user = userDao.get(staff.getUserSn());
			if (user != null) {
				staffDto.setLoginName(user.getLoginName());
			}
		}
		return staffDto;
	}
	
	public StaffDto getStaffByUserSn(Integer userSn) {
		StaffDto staffDto = null;
		MMStaff staff = staffDao.getStaffByUserSn(userSn);
		if (staff != null) {
			staffDto = BeanUtil.copyBeans(StaffDto.class, staff);
			// 取得用户部门数据
			MMDepartment dept = departmentStaffDao.getDepartment(staffDto.getStaffSn());
			if (dept != null) {
				staffDto.setDepartmentSn(dept.getDepartmentSn());
				staffDto.setDepartmentName(getDepartmentNamePath("", dept));
			} else {
				staffDto.setDepartmentSn(null);
				staffDto.setDepartmentName("");
			}
//			// 取得员工职位信息
//			if (staff.getPosition() != null) {
//				MMPosition position = positionDao.getPositionById(Integer.valueOf(staff.getPosition()));
//				staffDto.setPositionSn(position.getPositionSn());
//				staffDto.setPositionName(position.getPositionName());
//			}
		}
		return staffDto;
	}
	
	public StaffDto getStaffByLoginName(String loginName) {
		return staffDao.getStaffByLoginName(loginName);
	}
	
	public String del(Integer staffSn, String staffNo) {
		MMStaff staff = staffDao.getStaffByNo(staffNo);
		if (staff == null) {
			return "员工信息不存在，请刷新页面后重试。";
		}
		if (staff != null && !staff.getStaffSn().equals(staffSn)) {
			return "员工已变更，请刷新页面后重试。";
		}
		staffDao.del(staff);
		return Const.ResultCode.SUCCESS;
	}
	
	private String getDepartmentNamePath(String deptPath, MMDepartment dept) {
		String result = "/" + dept.getDepartmentName() + deptPath;
		if (dept.getParentDepartSn() > 1) {
			MMDepartment parentDept = departmentDao.get(dept.getParentDepartSn());
			result = getDepartmentNamePath(result, parentDept);
		}
		return result;
	}
	
}
